/**
 * Copyright (c) 2012 Fujian FUNO Mobile Communication Technology Co., Ltd.
 * All rights reserved by FUNO.
 */
package com.funo.ehealth.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.funo.ehealth.bean.HandMan;
import com.funo.ehealth.exception.SQLDOException;
import com.funo.ehealth.util.DBUtil;
import com.funo.ehealth.util.LogUtil;
import com.funo.ehealth.util.StringUtil;

/** 
 * 系统公共Dao
 * 
 * @version V1.1
 * @author zhangguangying
 * @date 2012-11-17
 */
public class CommonDao {
	
	/**
	 * CommonDao日志记录器
	 */
	private static final Log log = LogFactory.getLog(UserOrderDao.class);

	/**
	 * 查询维护人员列表
	 * @return ArrayList<HandMan>
	 */
	public ArrayList<HandMan> queryHandMan() {
		final String sql = "select * from handman";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		ArrayList<HandMan> handManList = new ArrayList<HandMan>();
		HandMan handMan = null;
		
		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn, sql);
			
			LogUtil.debug(log, "queryHandMan，参数：{sql:{}}", sql);
			res = pstmt.executeQuery();
			while(res.next()){
				handMan = new HandMan();
				handMan.setId(String.valueOf(res.getInt("id")));
				handMan.setName(res.getString("name"));
				handMan.setMobile(res.getString("mobile"));
				handMan.setPost(res.getString("post"));
				handMan.setCompany(res.getString("company"));
				handMan.setDescription(res.getString("description"));
				handManList.add(handMan);
			}
		} catch (SQLException e) {
//			LogUtil.error(log, e, "queryHandMan异常，参数：{sql:{}}", sql);
			throw new SQLDOException(StringUtil.handleString("queryHandMan异常，参数：{sql:{}}", sql), e);
		} finally {
			DBUtil.closeAll(conn, pstmt,res);
		}
		return handManList;
	}
	
	/**
	 * 批量插入警告信息到短信待发表
	 * @param list 维护人员列表
	 * @param content 发送内容
	 * @return int[]
	 */
	public int[] batchInsertSMSending(ArrayList<HandMan> list,String content){
		final String sql = "insert into smssending (id,servicelist_id,service_code,from_tel,user_tel,content,is_using) values (seq_smssending.nextval,?,?,?,?,?,?)";
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn, sql);
			
			LogUtil.debug(log, "总共有{}条记录待插入", list.size());
			for (HandMan handMan : list) {
				LogUtil.debug(log, "batchInsertSMSending，参数：{sql:{},mobile:{},content:{}}", sql,handMan.getMobile(),content);
				pstmt.setLong(1, -1);
				pstmt.setString(2, "");
				pstmt.setString(3, "10658237");
				pstmt.setString(4, handMan.getMobile());
				pstmt.setString(5, content);
				pstmt.setInt(6, 1);
				pstmt.addBatch();
			}

			int[] row = pstmt.executeBatch();
			int sum = 0;
			for (int i = 0; i < row.length; i++) {
				sum += row[i];
			}
			LogUtil.debug(log, "影响行数:{}", sum);
			return row;
		} catch (SQLException e) {
//			LogUtil.error(log, e, "batchInsertSMSending异常，参数：{sql:{},content:{}}", sql,content);
			throw new SQLDOException(StringUtil.handleString("batchInsertSMSending异常，参数：{sql:{},content:{}}", sql,content), e);
		} finally {
			DBUtil.closeAll(conn, pstmt, null);
		}
	}
}
